//
//  CJSkin.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-04-12.
//

import UIKit
import Foundation

public let kCJSkinKey = "CJSkinKey"
public let kCJSkinNotiKey = "CJSkinNotiKey"

open class CJSkinModel: NSObject {
    
    open var key: String = ""
    open var param: [String: String] = [:]
    open var params: [String] = []
    
    public override init() {
        super.init()
    }
    
    open class func model(key: String, param: [String: String]) -> CJSkinModel {
        let model = CJSkinModel()
        model.key = key
        model.param = param
        
        var params = [String]()
        for key in CJSkin.shared.skinKeys {
            let value = param[key] ?? ""
            params.append(value)
        }
        
        model.params = params
        return model
    }
    
    open class func model(sel: Selector, param: [String: String]) -> CJSkinModel {
        let key = NSStringFromSelector(sel)
        return model(key: key, param: param)
    }
    
    open class func model(key: String, params: [String]) -> CJSkinModel {
        let model = CJSkinModel()
        model.key = key
        model.params = params
        
        var param = [String : String]()
        for (index, key) in CJSkin.shared.skinKeys.enumerated() {
            var value = ""
            if index < (params.count - 1) {
                value = params[index];
            }
            param[key] = value;
        }
        
        model.param = param
        return model
    }
    
    open class func model(sel: Selector, params: [String]) -> CJSkinModel {
        let key = NSStringFromSelector(sel)
        return model(key: key, params: params)
    }
    
    /// 获取模型当前颜色
    open var color: UIColor {
        let value = param[CJSkin.shared.currentSkinKey]
        if let value = value {
            let color = UIColor.cj_color(value)
            return color
        } else {
            let color = UIColor.black
            return color
        }
    }
    
    /// 获取模型当前值
    open var value: String {
        let value = param[CJSkin.shared.currentSkinKey] ?? ""
        return value
    }
}


// MARK: - CJSkin

public enum CJSkinImageEx: Int {
    case plain
    case xcassets // 通过 xcassets 管理的图片
    case bundle // 通过 bundle 管理的图片
    /// 通过 xcassets 管理的图片
    /// 图片命名 name_(CJSkin.shared.currentSkinKey) ex. power_any, power_dark
    /// 如果设置 后缀别名 则使用对应的后缀别名 ex. power_a, power_d
    ///
    /// 通过 bundle 管理的图片
    /// bundle的名称为CJSkin.shared.currentSkinKey 例如 any.bundle, dark.bundle
}

// MARK: 皮肤管理器
open class CJSkin {
    
    /// 主题数组 默认@[@"any", @"light", @"dark"]
    open private(set) var skinKeys: [String] = ["any", "light", "dark"]
    /// 主题图片后缀别名数组 默认@[@"a", @"l", @"d"]
    open private(set) var skinImageSuffixKeys: [String] = ["a", "l", "d"]
    
    /// 当前主题 默认any
    open private(set) var currentSkinKey: String = "any"
    /// 当前主题的下标
    open private(set) var currentSkinIndex: Int = 0
    
    open var skinImageEx: CJSkinImageEx = .plain
    
    /// 主题颜色指定类
    /**
     @objc class var theme: CJSkinModel {
     let param = ["any": "DD2526", "dark": "A33D7F"]
     let key = #function
     let model = CJSkinModel.model(key: key, param: param)
     return model
     }
     注意: @objc 必须要
     */
    open var colorClass: AnyClass?
    
    /// 单例
    public static let shared = CJSkin()
    
    private init() {
        setupData()
    }
    
    private func setupData() {
        let skin = UserDefaults.standard.string(forKey: kCJSkinKey)
        if let skin = skin {
            currentSkinKey = skin
        }
        if skinKeys.contains(currentSkinKey) {
            currentSkinIndex = skinKeys.firstIndex(of: currentSkinKey) ?? 0
        }
        saveSkinKeyAndPostNoti()
    }
    
    public func setupSkinKeys(keys: [String]) {
        if keys == skinKeys {
            return
        }
        if keys.count > 0 {
            skinKeys = keys
            if skinKeys.contains(currentSkinKey) {
                currentSkinIndex = skinKeys.firstIndex(of: currentSkinKey) ?? 0
            } else {
                currentSkinKey = skinKeys[0]
                currentSkinIndex = 0
            }
            saveSkinKeyAndPostNoti()
        }
    }
    
    public func saveSkinKeyAndPostNoti() {
        UserDefaults.standard.set(self.currentSkinKey, forKey: kCJSkinKey)
        UserDefaults.standard.synchronize()
        postNoti()
    }
    
    public func postNoti() {
        NotificationCenter.default.post(name: .init(kCJSkinNotiKey), object: nil)
    }
    
    /// 设置主题
    @discardableResult
    open func setCurrentSkin(key: String) -> Bool {
        if skinKeys.contains(key) {
            currentSkinKey = key
            currentSkinIndex = skinKeys.firstIndex(of: currentSkinKey) ?? 0
            saveSkinKeyAndPostNoti()
            return true
        } else {
            kCJLog("[CJSkin] setCurrentSkin失败 key不在skinKeys内")
            return false
        }
    }
    
    /// 设置主题
    @discardableResult
    open func setCurrentSkin(index: Int) -> Bool {
        if index >= skinKeys.count {
            kCJLog("[CJSkin] setCurrentSkin失败 index不在skinKeys内")
            return false
        } else {
            let key = skinKeys[index]
            currentSkinKey = key
            currentSkinIndex = index
            saveSkinKeyAndPostNoti()
            return true
        }
    }
    
    /// 设置主题图片后缀别名数组
    open func setSkinImageSuffix(keys: [String]) -> Bool {
        if keys.count == skinKeys.count {
            skinImageSuffixKeys = keys
            return true
        } else {
            kCJLog("[CJSkin] setSkinImageSuffix失败 keys.count与skinKeys.count不一致")
            return false
        }
    }
    
    /// 当前主题图片后缀别名
    open func currentSkinImageSuffixKey() -> String {
        return skinImageSuffixKeys[currentSkinIndex]
    }
    
    
    
    
    
    
    
    
}

